package com.ymatou.iapi.prodstock.testcase;

import com.ymatou.iapi.prodstock.parameter.GetCatalogStockByProductInActivityIdBean;
import com.ymatou.iapi.prodstock.service.GetCatalogStockByProductInActivityIdCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.List;
import java.util.stream.Collectors;

/**
*查询规格活动库存
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetCatalogStockByProductInActivityId {
	private static GetCatalogStockByProductInActivityIdBean getcatalogstockbyproductinactivityidBean;
	private static GetCatalogStockByProductInActivityIdCall getcatalogstockbyproductinactivityidCall;
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productpriceSellerId"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询规格活动库存");
	}

	@Before
	public void caseUp() {
		getcatalogstockbyproductinactivityidBean=new GetCatalogStockByProductInActivityIdBean();
		getcatalogstockbyproductinactivityidCall=new GetCatalogStockByProductInActivityIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getcatalogstockbyproductinactivityidCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetCatalogStockByProductInActivityId_01() {
        Logger.start(true,"查询规格活动库存,单规格报名");
        try {
            //创建商品
            AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
            addpro.getProduct().getCatalogList().remove(0);
            // 设置商品库存,规格库存
            Integer catalogNum = 1000;
            addpro.getProduct().getCatalogList().forEach(catalog -> {
                catalog.setStock(catalogNum);
            });
            //获取商品ID，单规格
            String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
            //获取规格ID
            List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
            //创建一个活动  得到活动id
            Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
            //报名活动  获得商品在活动中的id
            int actNum = 50;
            List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, actNum,"10");
            //审核通过
            ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);

            getcatalogstockbyproductinactivityidBean.setProductInActivityId(Integer.parseInt(aids.get(0)));
            getcatalogstockbyproductinactivityidCall.setData(getcatalogstockbyproductinactivityidBean);
            getcatalogstockbyproductinactivityidCall.callService();
            Logger.verifyEquals("200",getcatalogstockbyproductinactivityidCall.getString("Code"),"验证code");
            new YmtCatalogsIWapper().selectCatalogsByProductId(productId).forEach(x->{
                getcatalogstockbyproductinactivityidCall.getCatalogStockList().forEach(p->{
                    if (x.getsCatalogId().equals(p.getCatalogId())){
                        Logger.verifyEquals(actNum,p.getStock(),"验证活动库存");
                    }
                });
            });

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetCatalogStockByProductInActivityId_02() {
        Logger.start(true,"查询规格活动库存,多规格报名");
        try {
            //创建商品
            AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
            // 设置商品库存,规格库存
            Integer catalogNum = 1000;
            addpro.getProduct().getCatalogList().forEach(catalog -> {
                catalog.setStock(catalogNum);
            });
            //获取商品ID，单规格
            String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
            //获取规格ID
            List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
            //创建一个活动  得到活动id
            Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
            //报名活动  获得商品在活动中的id
            int actNum = 50;
            List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, actNum,"10");
            //审核通过
            ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);

            getcatalogstockbyproductinactivityidBean.setProductInActivityId(Integer.parseInt(aids.get(0)));
            getcatalogstockbyproductinactivityidCall.setData(getcatalogstockbyproductinactivityidBean);
            getcatalogstockbyproductinactivityidCall.callService();
            Logger.verifyEquals("200",getcatalogstockbyproductinactivityidCall.getString("Code"),"验证code");
            new YmtCatalogsIWapper().selectCatalogsByProductId(productId).forEach(x->{
                getcatalogstockbyproductinactivityidCall.getCatalogStockList().forEach(p->{
                    if (x.getsCatalogId().equals(p.getCatalogId())){
                        Logger.verifyEquals(actNum,p.getStock(),"验证活动库存");
                    }
                });
            });

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetCatalogStockByProductInActivityId_03() {
        Logger.start(true,"查询规格活动库存,ProductInActivityId为0");
        try {
            getcatalogstockbyproductinactivityidBean.setProductInActivityId(0);
            getcatalogstockbyproductinactivityidCall.setData(getcatalogstockbyproductinactivityidBean);
            getcatalogstockbyproductinactivityidCall.callService();
            Logger.verifyEquals("201",getcatalogstockbyproductinactivityidCall.getString("Code"),"验证code");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
}